<template>
	<div class="dp-text-ellipsis-wrapper">
		<div class="text" :class="textClass" :style="textStyleObject">
			<label class="btn" @click="showClick"></label>
			{{ info }}
		</div>
	</div>
</template>
<script>
	export default {
		name: 'DpTextEllipsis',
		props: {
			info: {
				type: String,
				default: '',
			},
			lineClamp: {
				type: Number,
				default: 2,
			},
			hiddenBtn: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				showall: false,
			}
		},
		computed: {
			textStyleObject() {
				return {
					'max-height': this.showall ? 'none' : `${1.5 * this.lineClamp}em`,
				}
			},
			textClass() {
				let cls = this.showall ? 'showall' : ''
				cls = cls + (this.hiddenBtn ? ' hidden-btn' : '')
				return cls
			},
		},
		watch: {
			info() {
				this.showall = false
			},
		},
		methods:{
			showClick(){
				this.$emit('clickshow')
			}
		}
	}
</script>
<style lang="scss">
	.dp-text-ellipsis-wrapper {
		display: flex;
		overflow: hidden;
		font-size: 14px;
		line-height: 20px;

		.text {
			position: relative;
			overflow: hidden;
			line-height: 1.5;
			text-align: justify;
			text-overflow: ellipsis;
			word-break: break-all;
			transition: 0.3s max-height;
			min-width: 100%;
		}

		.text::before {
			float: right;
			height: calc(100% - 20px);
			content: '';
		}

		.text::after {
			position: absolute;
			width: 999vw;
			height: 999vw;
			margin-left: -100px;
			content: '';
		}

		.btn {
			position: relative;
			float: right;
			clear: both;
			margin-left: 10px;
			font-size: 14px;
			padding: 0 8px;
			color: #206ef7;
			line-height: 20px;
			border-radius: 4px;
			cursor: pointer;
			z-index: 10;
		}

		.btn::after {
			/* stylelint-disable-next-line */
			font-family: element-icons !important;
			content: '【展开】';
		}

		.text.showall {
			max-height: none;
		}

		.text.showall .btn::before {
			visibility: hidden;
		}

		.text.showall .btn::after {
			visibility: hidden;
		}

		.text.showall .btn::after {
			content: '【收起】';
			visibility: visible;
		}

		.btn::before {
			position: absolute;
			left: 1px;
			color: #333;
			transform: translateX(-100%);
			content: '...';
		}
	}
</style>